package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.Painter;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompConector._arrowType;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Point;

public class CompLineConector extends CompConector {

	private static final float TOUCH_DIF = 0.01f;

	public CompLineConector(int fatherID, int childID) {
		super(fatherID, childID);
	}

	@Override
	public void CalcularConexiones(Componente father, Componente child) {
		_fatherCenter = father.get_centerPoint();
		_childCenter = child.get_centerPoint();

		int Ax = father.Anchor.x;
		int AxWith = (int) (Ax + father.get_width());
		int Ay = father.Anchor.y;
		int AyHeight = (int) (Ay + father.get_width());
		// Ubicar Cuadrante

		_numberOfPoints = 2;

		_points[0] = father.requestConnectionPoint(this, 1, _childCenter);
		_points[1] = child.requestConnectionPoint(this, 2, _fatherCenter);
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		// Dibujamos las lineas del conector
		Path LinePath = new Path();
		LinePath.moveTo(((_points[0]).x + offSetX) * globalScale,
		((_points[0]).y + offSetY) * globalScale);
		LinePath.lineTo(((_points[1]).x + offSetX) * globalScale, ((_points[1]).y + offSetY) * globalScale);
		c.drawPath(LinePath,
				Painter.getInstancia().getLineTypePaint(_currentState,
						_currentLineType));
		
		/*switch (_currentLineType) {
		case DASHED_TYPE:
			c.drawLine(
					((_points[0]).x + offSetX) * globalScale,
					((_points[0]).y + offSetY) * globalScale,
					((_points[1]).x + offSetX) * globalScale,
					((_points[1]).y + offSetY) * globalScale,
					Painter.getInstancia().getLineTypePaint(_currentState,
							DASHED_TYPE));
			break;

		default:
			c.drawLine(
					((_points[0]).x + offSetX) * globalScale,
					((_points[0]).y + offSetY) * globalScale,
					((_points[1]).x + offSetX) * globalScale,
					((_points[1]).y + offSetY) * globalScale,
					Painter.getInstancia().getLineTypePaint(_currentState,
							NORMAL_TYPE));
			break;
		}*/

		_normalLinePaint = Painter.getInstancia().getLineTypePaint(
				_currentState, _currentLineType);

		// Dibujamos las flechas
		DibujarFlecha(c, globalScale, offSetX, offSetY);

		// Dibujamos el consto de ser necesario
		if (_haveCost) {
			if((int)costo == costo ){
				c.drawText(Integer.toString((int)costo),
						(((_points[0].x + _points[1].x) / 2) + 10 + offSetX)
								* globalScale,
						(((_points[0].y + _points[1].y) / 2) + 10 + offSetY)
								* globalScale,
						Painter.getInstancia().getTextPaint(false, globalScale));
			}else{
				c.drawText(Float.toString(costo),
						(((_points[0].x + _points[1].x) / 2) + 10 + offSetX)
								* globalScale,
						(((_points[0].y + _points[1].y) / 2) + 10 + offSetY)
								* globalScale,
						Painter.getInstancia().getTextPaint(false, globalScale));
			}
		}
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		float DisX = (_points[0]).x - (_points[1]).x;
		float DisY = (_points[0]).y - (_points[1]).y;

		// Distancia entre los 2 puntos del conector
		double DistTotal = Math.sqrt((DisX * DisX) + (DisY * DisY));

		// Distancia entre el toque y el punto 1
		DisX = (_points[0]).x - touch.x;
		DisY = (_points[0]).y - touch.y;
		double DistPunto1 = Math.sqrt((DisX * DisX) + (DisY * DisY));

		// Distancia entre el toque y el punto 2
		DisX = (_points[1]).x - touch.x;
		DisY = (_points[1]).y - touch.y;
		double DistPunto2 = Math.sqrt((DisX * DisX) + (DisY * DisY));

		if ((DistPunto1 + DistPunto2) - DistTotal < TOUCH_DIF * DistTotal) {
			respuesta = true;
		}

		return respuesta;
	}

	public void DibujarFlecha(Canvas c, float globalScale, int offSetX,
			int offSetY) {
		Point arrowBot = new Point();
		Point arrowTop = new Point();
		if (_fatherArrowType != _arrowType.Asociacion) {
			float dX = (_childCenter.x - _fatherCenter.x);
			float dY = (_childCenter.y - _fatherCenter.y);
			if (dY == 0) {
				// return otherCompCenter;
			} else {
				double m = dY / dX;
				double angle = Math.atan(m);
				if (_fatherCenter.x > _childCenter.x) {
					arrowBot.y = _points[0].y
							- (int) (Math.sin(angle - 24.5) * 20);
					arrowBot.x = _points[0].x
							- (int) (Math.cos(angle - 24.5) * 20);
					arrowTop.x = _points[0].x
							- (int) (Math.cos(angle + 24.5) * 20);
					arrowTop.y = _points[0].y
							- (int) (Math.sin(angle + 24.5) * 20);
					c.drawLine(
							(_points[0].x + offSetX) * globalScale,
							(_points[0].y + offSetY) * globalScale,
							(arrowBot.x + offSetX) * globalScale,
							(arrowBot.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
					c.drawLine(
							(_points[0].x + offSetX) * globalScale,
							(_points[0].y + offSetY) * globalScale,
							(arrowTop.x + offSetX) * globalScale,
							(arrowTop.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
				} else {
					arrowBot.y = _points[0].y
							+ (int) (Math.sin(angle - 24.5) * 20);
					arrowBot.x = _points[0].x
							+ (int) (Math.cos(angle - 24.5) * 20);
					arrowTop.x = _points[0].x
							+ (int) (Math.cos(angle + 24.5) * 20);
					arrowTop.y = _points[0].y
							+ (int) (Math.sin(angle + 24.5) * 20);
					c.drawLine(
							(_points[0].x + offSetX) * globalScale,
							(_points[0].y + offSetY) * globalScale,
							(arrowBot.x + offSetX) * globalScale,
							(arrowBot.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
					c.drawLine(
							(_points[0].x + offSetX) * globalScale,
							(_points[0].y + offSetY) * globalScale,
							(arrowTop.x + offSetX) * globalScale,
							(arrowTop.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
				}
			}
		}
		if (_childArrowType != _arrowType.Asociacion) {
			float dX = (_childCenter.x - _fatherCenter.x);
			float dY = (_childCenter.y - _fatherCenter.y);
			if (dY == 0) {
				// return otherCompCenter;
			} else {
				double m = dY / dX;
				double angle = Math.atan(m);
				if (_fatherCenter.x > _childCenter.x) {
					arrowBot.y = _points[1].y
							+ (int) (Math.sin(angle - 24.5) * 20);
					arrowBot.x = _points[1].x
							+ (int) (Math.cos(angle - 24.5) * 20);
					arrowTop.x = _points[1].x
							+ (int) (Math.cos(angle + 24.5) * 20);
					arrowTop.y = _points[1].y
							+ (int) (Math.sin(angle + 24.5) * 20);
					c.drawLine(
							(_points[1].x + offSetX) * globalScale,
							(_points[1].y + offSetY) * globalScale,
							(arrowBot.x + offSetX) * globalScale,
							(arrowBot.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
					c.drawLine(
							(_points[1].x + offSetX) * globalScale,
							(_points[1].y + offSetY) * globalScale,
							(arrowTop.x + offSetX) * globalScale,
							(arrowTop.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
				} else {
					arrowBot.y = _points[1].y
							- (int) (Math.sin(angle - 24.5) * 20);
					arrowBot.x = _points[1].x
							- (int) (Math.cos(angle - 24.5) * 20);
					arrowTop.x = _points[1].x
							- (int) (Math.cos(angle + 24.5) * 20);
					arrowTop.y = _points[1].y
							- (int) (Math.sin(angle + 24.5) * 20);
					c.drawLine(
							(_points[1].x + offSetX) * globalScale,
							(_points[1].y + offSetY) * globalScale,
							(arrowBot.x + offSetX) * globalScale,
							(arrowBot.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
					c.drawLine(
							(_points[1].x + offSetX) * globalScale,
							(_points[1].y + offSetY) * globalScale,
							(arrowTop.x + offSetX) * globalScale,
							(arrowTop.y + offSetY) * globalScale,
							Painter.getInstancia().getArrowPaint(_currentState,
									false));
				}
			}
		}
	}

	@Override	
	public String generarDOM() {
		String DOM = "\t<LineConector IDPadre=\"" + _fatherId + "\" IDHijo=\""
				+ _childId + "\" FlechaPadre=\"" + _fatherArrowType + "\" FlechaHijo=\""
				+ _childArrowType + "\" OrientacionPadre=\""
				+ _fatherOrientation + "\" OrientacionHijo=\""
				+ _childOrientation + "\" TipoLinea=\"" + _currentLineType
				+ "\" Costo=\"" + costo
				+ "\" HaveCost=\"" + Boolean.toString(_haveCost)
				+ "\">\n";
		for (int i = 0; i < _numberOfPoints; i++) {
			DOM = DOM + "\t\t<Punto X=\"" + _points[i].x + "\" Y=\""
					+ _points[i].y + "\" />\n";
		}
		DOM = DOM + "\t</LineConector>\n";
		return DOM;
	}
}
